var cajaArray = new Array();
var boxIndex = 0;
var numberOfColumns = 0;
var share_estado;
//////////////////////////////////////////
var transparencyWhenDragging = false;

var autoScrollSpeed = 4;	// Autoscroll speed	- Higher = faster	
var dragObjectBorderWidth = 1;	// Border size of your RSS boxes - used to determine width of dotted rectangle

/* END USER VARIABLES */	
var autoScrollActive = false;

var dragDropCounter = -1;
var dragObject = false;
var dragObjectNextSibling = false;
var dragObjectParent = false;
var destinationObj = false;
var dragableBoxesArray = new Array();

var mouse_x;
var mouse_y;

var mov_x;

var el_x;
var el_y;	

var rectangleDiv;
var okToMove = true;

var documentHeight = false;
var documentScrollHeight = false;
var dragableAreaWidth = false;
	
var opera = navigator.userAgent.toLowerCase().indexOf('opera')>=0?true:false;

function autoScroll(direction,yPos)
{
	if(document.documentElement.scrollHeight>documentScrollHeight && direction>0)return;
	if(opera)return;
	window.scrollBy(0,direction);
	if(!dragObject)return;
	
	if(direction<0){
		if(document.documentElement.scrollTop>0){
			dragObject.style.top = (el_y - mouse_y + yPos + document.documentElement.scrollTop) + 'px';		
		}else{
			autoScrollActive = false;
		}
	}else{
		if(yPos>(documentHeight-50)){	
			dragObject.style.top = (el_y - mouse_y + yPos + document.documentElement.scrollTop) + 'px';			
		}else{
			autoScrollActive = false;
		}
	}
	if(autoScrollActive)setTimeout('autoScroll('+direction+',' + yPos + ')',3);
}
	
function initDragDropBox(e)
{
	dragDropCounter = 1;
	if(document.all)e = event;
	
	if (e.target) source = e.target;
		else if (e.srcElement) source = e.srcElement;
		if (source.nodeType == 3) // defeat Safari bug
			source = source.parentNode;
	
	if(source.tagName.toLowerCase()=='img' || source.tagName.toLowerCase()=='a' || source.tagName.toLowerCase()=='input' || source.tagName.toLowerCase()=='td' || source.tagName.toLowerCase()=='tr' || source.tagName.toLowerCase()=='table')return;
	
	mov_x = e.pageX;

	mouse_x = e.clientX;
	mouse_y = e.clientY;	
	var numericId = this.id.replace(/[^0-9]/g,'');
	el_x = getLeftPos(this.parentNode.parentNode)/1;
	el_y = getTopPos(this.parentNode.parentNode)/1 - document.documentElement.scrollTop;
		
	dragObject = this.parentNode.parentNode;
	
	documentScrollHeight = document.documentElement.scrollHeight + 100 + dragObject.offsetHeight;
	
	
	if(dragObject.nextSibling){
		dragObjectNextSibling = dragObject.nextSibling;
		if(dragObjectNextSibling.tagName!='DIV')dragObjectNextSibling = dragObjectNextSibling.nextSibling;
	}
	dragObjectParent = dragableBoxesArray[numericId]['parentObj'];
		
	dragDropCounter = 0;
	initDragDropBoxTimer();	
	
	return false;
}


function initDragDropBoxTimer()
{
	if(dragDropCounter>=0 && dragDropCounter<10){
		dragDropCounter++;
		setTimeout('initDragDropBoxTimer()',3);
		return;
	}
	if(dragDropCounter==10){
		mouseoutBoxHeader(false,dragObject);
	}
	
}

function moveDragableElement(e){
	if(document.all)e = event;
	if(dragDropCounter<10)return;
	
	if(document.all && e.button!=1 && !opera){
		stop_dragDropElement();
		return;
	}
	
	
	if(document.body!=dragObject.parentNode){
		dragObject.style.width = '23%';
		dragObject.style.position = 'absolute';	
		dragObject.style.textAlign = 'left';
		if(transparencyWhenDragging){	
			dragObject.style.filter = 'alpha(opacity=70)';
			dragObject.style.opacity = '0.7';
		}	
		dragObject.parentNode.insertBefore(rectangleDiv,dragObject);
		rectangleDiv.style.display='block';
		document.body.appendChild(dragObject);

		rectangleDiv.style.width = dragObject.parentNode.style.width;
		rectangleDiv.style.height = (dragObject.offsetHeight - (dragObjectBorderWidth*2)) + 'px'; 
		
	}
	
	if(e.clientY<50 || e.clientY>(documentHeight-50)){
		if(e.clientY<50 && !autoScrollActive){
			autoScrollActive = true;
			autoScroll((autoScrollSpeed*-1),e.clientY);
		}
		
		if(e.clientY>(documentHeight-50) && document.documentElement.scrollHeight<=documentScrollHeight && !autoScrollActive){
			autoScrollActive = true;
			autoScroll(autoScrollSpeed,e.clientY);
		}
	}else{
	   autoScrollActive = false;
	}		

	var leftPos = e.clientX;
	var topPos = e.clientY + document.documentElement.scrollTop;
	
	dragObject.style.left = (e.clientX - 160) + 'px';
	dragObject.style.top = (el_y - mouse_y + e.clientY + document.documentElement.scrollTop) + 'px';					

	if(!okToMove)return;
	okToMove = false;

	destinationObj = false;	
	rectangleDiv.style.display = 'none'; 
	
	var objFound = false;
	var tmpParentArray = new Array();
	
	if(!objFound){
		for(var no=1;no<auxiliar.length;no++){
			if(dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj']==dragObject)continue;
			tmpParentArray[dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj'].parentNode.id] = true;
			if(!objFound){
				var tmpX = getLeftPos(dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj']);
				var tmpY = getTopPos(dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj']);

				if(leftPos>tmpX && leftPos<(tmpX + dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj'].offsetWidth) && topPos>(tmpY-20) && topPos<(tmpY + (dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj'].offsetHeight/2))){
					destinationObj = dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj'];
					destinationObj.parentNode.insertBefore(rectangleDiv,dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj']);
					rectangleDiv.style.display = 'block';
					objFound = true;
					break;
					
				}
				
				if(leftPos>tmpX && leftPos<(tmpX + dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj'].offsetWidth) && topPos>=(tmpY + (dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj'].offsetHeight/2)) && topPos<(tmpY + dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj'].offsetHeight)){
					objFound = true;
					if(dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj'].nextSibling){
						
						destinationObj = dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj'].nextSibling;
						if(!destinationObj.tagName)destinationObj = destinationObj.nextSibling;
						if(destinationObj!=rectangleDiv)destinationObj.parentNode.insertBefore(rectangleDiv,destinationObj);
					}else{
						destinationObj = dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj'].parentNode;
						dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj'].parentNode.appendChild(rectangleDiv);
					}
					rectangleDiv.style.display = 'block';
					break;					
				}
				
				
				if(!dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj'].nextSibling && leftPos>tmpX && leftPos<(tmpX + dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj'].offsetWidth)
				&& topPos>topPos>(tmpY + (dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj'].offsetHeight))){
					destinationObj = dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj'].parentNode;
					dragableBoxesArray[auxiliar[no]['cajaIndex']]['obj'].parentNode.appendChild(rectangleDiv);	
					rectangleDiv.style.display = 'block';	
					objFound = true;				
					
				}
			}
			
		}
	
	}
	
	if(!objFound){
		
		for(var no=1;no<=numberOfColumns;no++){
			if(!objFound){
				var obj = document.getElementById('dragableBoxesColumn' + no);			
				
					var left = getLeftPos(obj)/1;						
				
					var width = obj.offsetWidth;
					if(leftPos>left && leftPos<(left+width)){
						destinationObj = obj;
						obj.appendChild(rectangleDiv);
						rectangleDiv.style.display='block';
						objFound=true;		
						
					}				
				
			}
		}		
		
	}


	setTimeout('okToMove=true',3);
	
}

function stop_dragDropElement()
{
	var indefinido = 0;
	var nrocajaxdebajo = 0;
	var escolumna = 0;
	
	
	if(dragDropCounter<10){
		dragDropCounter = -1
		return;
	}
	dragDropCounter = -1;
	if(transparencyWhenDragging){
		dragObject.style.filter = null;
		dragObject.style.opacity = null;
	}		
	
	dragObject.style.position = 'static';
	dragObject.style.width = null;
	var numericId = dragObject.id.replace(/[^0-9]/g,'');
	if(destinationObj && destinationObj.id!=dragObject.id && destinationObj.id != 'rectangleDiv'){
		
		if(destinationObj.id.indexOf('dragableBoxesColumn')>=0){
			destinationObj.appendChild(dragObject);
			dragableBoxesArray[numericId]['parentObj'] = destinationObj;
			escolumna=destinationObj.id.replace(/[^0-9]/g,'');
		}else{
			destinationObj.parentNode.insertBefore(dragObject,destinationObj);
			dragableBoxesArray[numericId]['parentObj'] = destinationObj.parentNode;
			nrocajaxdebajo=destinationObj.id.replace(/[^0-9]/g,'');
		}
						
	}else{
		if(dragObjectNextSibling){
			dragObjectParent.insertBefore(dragObject,dragObjectNextSibling);
		}else{
			dragObjectParent.appendChild(dragObject);
		}				
	}
	
	var columna = dragObject.parentNode.id.replace(/[^0-9]/g,'');
	
	autoScrollActive = false;
	rectangleDiv.style.display = 'none'; 
	dragObject = false;
	dragObjectNextSibling = false;
	destinationObj = false;

	documentHeight = document.documentElement.clientHeight;
	
	if(indefinido != 1)
	{
		var dir = '/movimientowidget/?numerocolumna='+ columna +'&numerocaja='+ numericId +'&cajaunder='+nrocajaxdebajo+'&moveracolumna='+escolumna;	
		//alert(dir)
		obtenerDatosAjax(dir);
	}
}

function getTopPos(inputObj)
{		
  var returnValue = inputObj.offsetTop;
  while((inputObj = inputObj.offsetParent) != null){
	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
  }
  return returnValue;
}

function getLeftPos(inputObj)
{
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null){
	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
  }
  return returnValue;
}


function createHelpObjects()
{
	/* Creating rectangle div */
	rectangleDiv = document.createElement('DIV');
	rectangleDiv.id='rectangleDiv';
	rectangleDiv.style.display='none';
	document.body.appendChild(rectangleDiv);	 
}

function cancelSelectionEvent(e)
{
	if(document.all)e = event;
	
	if (e.target) source = e.target;
		else if (e.srcElement) source = e.srcElement;
		if (source.nodeType == 3) // defeat Safari bug
			source = source.parentNode;
	if(source.tagName.toLowerCase()=='input')return true;
					
	if(dragDropCounter>=0)return false; else return true;	
	
}

function cancelEvent()
{
	return false;
}

function initEvents()
{
	document.body.onmousemove = moveDragableElement;
	document.body.onmouseup = stop_dragDropElement;
	document.body.onselectstart = cancelSelectionEvent;

	document.body.ondragstart = cancelEvent;	
	
	documentHeight = document.documentElement.clientHeight;	
}
